﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication1.DAO;
using COMExcel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void biênBảnCoiThiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new BienBanCoiThi();
            frm.Show();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            LoadNamHoc();
            LoadHocKy();
        }

        private void LoadNamHoc()
        {
            List<NamHocDTO> ds = NamHocDAO.GetList();
            foreach (NamHocDTO nam in ds)
                cbNamHoc.Items.Add(nam);
            cbNamHoc.SelectedIndex = 0;
        }

        private void LoadHocKy()
        {
            List<HocKyDTO> ds = HocKyDAO.GetList();
            foreach (HocKyDTO hk in ds)
                cbHocKy.Items.Add(hk);
            cbHocKy.SelectedIndex = 0;
        }

        private void nhapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new BienBanCoiThi();
            frm.Show();
        }

        private void nhậpCôngViệcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new CongViec();
            frm.Show();
        }

        private void btChamCong_Click(object sender, EventArgs e)
        {
            int idNamHoc = ((NamHocDTO)cbNamHoc.SelectedItem).id;
            int idHocky = ((HocKyDTO)cbHocKy.SelectedItem).id;

            ShowResult(ChamCongDAO.ChamCong(idNamHoc, idHocky));

        }

        private void ShowResult(List<DTO.ChamCongDTO> list)
        {
            dgChamCong.Rows.Clear();
            for (int i = 0 ; i< list.Count; i++)
            {
                string[] s = list[i].ToStrings();
                s[0] = (i+1).ToString();
                dgChamCong.Rows.Add(s);
            }
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btXuatExcel_Click(object sender, EventArgs e)
        {
            
            COMExcel.Application xlApp;
            COMExcel.Workbook xlWorkBook;
            COMExcel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (COMExcel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            xlWorkSheet.Activate();
            xlWorkSheet.Name = "Export_Cham_Cong";

            COMExcel.Range r = (COMExcel.Range)xlWorkSheet.get_Range("A1", "A4");
            r.Value2 = "My Value";
            r.Columns.AutoFit();

            // Ẩn chương trình
            //xlApp.Visible = false;

            xlWorkBook.SaveAs("C:\\Export_Cham_Cong.xls", COMExcel.XlFileFormat.xlWorkbookNormal, null, null, false, false,
                COMExcel.XlSaveAsAccessMode.xlExclusive, false, false, false, false, false);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            xlWorkBook.Close(false, false, false);
            xlApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

            MessageBox.Show("Đã tạo xong file excel, bạn có thể xem ở c:\\Export_Cham_Cong.xls");
        }

    }
}
